package com.dirox.plugins.issuetab;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.jql.builder.JqlClauseBuilder;
import com.atlassian.jira.jql.builder.JqlQueryBuilder;
import com.atlassian.jira.plugin.webfragment.contextproviders.AbstractJiraContextProvider;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.web.bean.PagerFilter;
//import com.atlassian.sal.api.user.UserManager;
import com.atlassian.jira.user.util.UserManager;
import com.dirox.plugins.ao.entities.Note;
import com.dirox.plugins.ao.service.NoteService;
import com.dirox.plugins.utils.NoteComparator;

public class NoteProvider extends AbstractJiraContextProvider {
	private final JiraAuthenticationContext context;
	private NoteService noteService;
	private UserManager userManager;
	private IssueService issueService;
	private SearchService searchService;
	private ProjectManager projectManager;

	

	public NoteProvider(JiraAuthenticationContext context,
			NoteService noteService, UserManager userManager,
			IssueService issueService, SearchService searchService,
			ProjectManager projectManager) {
		super();
		this.context = context;
		this.noteService = noteService;
		this.userManager = userManager;
		this.issueService = issueService;
		this.searchService = searchService;
		this.projectManager = projectManager;
	}
	public List<Issue> getIssues(String projectName) {
		final User currentUser = context.getLoggedInUser();
		JqlClauseBuilder jqlClauseBuilder = JqlQueryBuilder.newClauseBuilder();
		com.atlassian.query.Query query = jqlClauseBuilder.project(projectName)
				.buildQuery();

		PagerFilter pagerFilter = PagerFilter.getUnlimitedFilter();
		com.atlassian.jira.issue.search.SearchResults searchResults = null;
		try {
			searchResults = searchService.search(currentUser, query,
					pagerFilter);
		} catch (SearchException e) {
			e.printStackTrace();
		}
		return searchResults.getIssues();
	}

	public List<Issue> getIssues() {
		List<Issue> issues = new ArrayList<Issue>();
		List<Project> projects = projectManager.getProjectObjects();
		for (Project p : projects) {
			issues.addAll(getIssues(p.getName()));
		}
		return issues;
	}


	@Override
	public Map getContextMap(User arg0, JiraHelper helper) {
		Map contextMap = new HashMap();
		final User currentUser = context.getLoggedInUser();
		List<Issue> issues =  getIssues();
		Issue currentIssue = (Issue) helper.getContextParams().get("issue");
		List<Note> notes = noteService.getNotes(currentIssue.getKey());
		List<Note> memos = noteService.getMemos(currentIssue.getKey());
		List<Note> notesAndMemos = new ArrayList<Note>();
		notesAndMemos.addAll(memos);
		notesAndMemos.addAll(notes);
		
		Collections.sort(notesAndMemos, new NoteComparator());
		int lastIndex = notesAndMemos.size() -1;
		
		List<User> users = new ArrayList<User>();
		users.addAll(userManager.getAllUsers());
		users.remove(currentUser);
		
		contextMap.put("curIssue", currentIssue);
		contextMap.put("currentUser", currentUser);
		if (notesAndMemos.size() > 0) {
			contextMap.put("notesAndMemos", notesAndMemos.get(lastIndex));
			contextMap.put("hasLastNote", 1);
		}
		else {
			contextMap.put("hasLastNote", 0); 
		}
		contextMap.put("memos", memos);
		contextMap.put("notes", notes);
		contextMap.put("issues", issues);
		contextMap.put("users", users);
		return contextMap;
	}

}
